<template>
  <div class="chartPart"></div>
</template>

<script>
import myEcharts from '@/utils/echarts/echarts-util.js'
import mixinsData from './mixins_data'
export default {
  props: {
    showTitle: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    subText: {
      type: String,
      default: ''
    },
    showLegend: {
      type: Boolean,
      default: false
    },
    // x轴数据
    xData: {
      type: Array,
      required: true
    },
    lineColor: {
      type: Array,
      default: () => []
    },
    chartData: {
      type: Array,
      required: true
    },
    otherOptions: {
      type: Object,
      default: () => {}
    },
    commonStyle: {
      type: Object,
      default: () => {}
    }
  },
  mixins: [mixinsData],
  data () {
    return {
      chart: null
    }
  },
  methods: {
    initChart () {
      this.chart = this.$echarts.init(this.$el, 'macarons')
      this.chart.off()
      this.setOptions()
      let that = this
      window.addEventListener('resize', () => {
        that.chart.resize()
      })
    },
    setOptions () {
      this.chart.showLoading()
      const datas = myEcharts.EchartsDataFormate.groupFormatter(this.chartData, 'line')
      this.chart.hideLoading()
      // 统一样式
      if (JSON.stringify(this.commonStyle) !== '{}') {
        for (let item in datas.data) {
          datas.data[item] = Object.assign(datas.data[item], this.commonStyle)
        }
      }
      const option = {
        color: this.lineColor,
        tooltip: {
          trigger: 'axis',
          formatter: function (params) {
            localStorage.setItem('clickName', params[0].name)
          }
        },
        grid: {
          top: '25%',
          left: '4%',
          right: '5%',
          bottom: '8%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.xData,
          axisLabel: {
            textStyle: {
              color: '#262626'
            },
            formatter: function (params) {
              return params.split(' ')[0]
            }
          }
        },
        yAxis: {
          name: '',
          type: 'value',
          axisLabel: {
            formatter: '{value}',
            textStyle: {
              color: '#262626'
            }
          },
          splitLine: {
            lineStyle: {
              type: 'dashed',
              color: 'rgb(23,255,243,0.3)'
            }
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: 'rgb(0,253,255,0.6)'
            }
          }
        },
        series: datas.data
      }
      if (this.showLegend) {
        option.legend = {
          textStyle: {
            fontSize: 12,
            // color: 'rgb(0,253,255)'
            color: '#262626'
          },
          right: '4%',
          data: datas.group
        }
      }
      this.chart.setOption(option, true)
    }
  }
}
</script>
